/* XKCDScraper.cpp
Copyright (c) 2008 Michael Zahniser
Please note the license terms (MIT license) at the end of this document.

Function definitions for the XKCDScraper class.
*/

#include "XKCDScraper.h"

#include <sstream>

using std::ostringstream;
using std::istringstream;



// Constructor:
XKCDScraper::XKCDScraper()
	: description("XKCD by Randall Munroe (xkcd.com)")
{
	SetIndex(GetLastIndex());
}



// DoClone:
// Make a copy of this Scraper.
Scraper *XKCDScraper::DoClone() const
{
	XKCDScraper *scraper = new XKCDScraper();
	scraper->description = description;
	scraper->path = path;
	
	return scraper;
}



// GetLastIndex:
// Get the index for today's date.
int XKCDScraper::GetLastIndex() const
{
	// There's no way to tell what the latest XKCD comic is, without loading the xkcd page.
	return -2;
}



// GetPath:
// Get the URL of the image for the given index.
string XKCDScraper::GetPath(int index) const
{
	return path;
}



// GetDescription:
// Get the description string to display for this comic, for this index.
string XKCDScraper::GetDescription(int index) const
{
	return description;
}



// LoadIndex:
// Load the comic for the given index into the given buffer, using the 
// given internet connection.
bool XKCDScraper::LoadIndex(int index, string &buffer, Curl &curl)
{
	if(index < 0)
	{
		string page;
		string path = "http://xkcd.com/";
		Fetch(path, page, curl);
		
		size_t pos = page.find("Permanent link");
		pos = page.find(path, pos) + path.length();
		size_t end = page.find('/', pos);
		string number = page.substr(pos, end - pos);
		
		istringstream(number) >> index;
		SetIndex(index);
	}
	// Comic 404 does not exist - a little XKCD joke that makes this a bit more
	// complicated than it would otherwise be.
	if(index == 404)
	{
		description = "404 - Not Found.";
		path = "";
		buffer.clear();
		SetIndex(index);
		return true;
	}
	
	// Load the entire XKCD page for the given index.
	ostringstream pathStr;
	pathStr<<"http://xkcd.com/"<<index<<"/";
	
	path = pathStr.str();
	
	string page;
	Fetch(path, page, curl);

	size_t pathPos = page.find("http://imgs.xkcd.com/comics/");
	if(pathPos == string::npos)
		return false;
	
	// Extract the image path.
	size_t pathEnd = page.find('"', pathPos);
	path.assign(page, pathPos, pathEnd - pathPos);
	
	// Find the "title" (mouse-over text) of the image.
	size_t titlePos = page.find("title=\"", pathPos);
	ostringstream out;
	out << "XKCD by Randall Munroe (xkcd.com) comic " << index << ".";
	description = out.str();
	if(titlePos != string::npos)
	{
		titlePos += 7;
		size_t titleEnd = page.find('"', titlePos);
		description.assign(page, titlePos, titleEnd - titlePos);
		description = out.str() + " (\"" + description + "\")";
	}
	
	// Find the name of the image (title of the page).
	size_t altPos = page.find("alt=\"", pathPos);
	if(titlePos != string::npos)
	{
		altPos += 5;
		size_t altEnd = page.find('"', altPos);
		string alt(page, altPos, altEnd - altPos);
		description = "\"" + alt + "\" - " + description;
	}

	Fetch(path, buffer, curl);

	// Check that the image was loaded.
	if(path.empty())
		return false;
	
	SetIndex(index);
	
	return true;
}



/* Copyright (c) 2008 Michael Zahniser

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE. */
